package com.dys.testqueue16;

import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.TimeUnit;

/**
 * @className: com.dys.testqueue16.SynchronousQueueTest
 * @description: com.dys.testqueue16
 * @author: pine cone
 * @version: v1.0.0
 * @createTime: 2024/08/12 下午10:38
 */
public class SynchronousQueueTest {
    /**
     * 这是一个main()方法,是应用程序的入口
     *
     * @param args 方法的形参列表,是一个字符串数组,包含了从命令行传递给程序的参数
     */
    public static void main(String[] args) throws InterruptedException {
        new SynchronousQueueTest().test4();
    }

    /**
     * 添加元素
     *
     * @throws InterruptedException
     */
    public void test1() throws InterruptedException {
        SynchronousQueue<String> sq = new SynchronousQueue<String>();
        // 因为队列没有容量,抛出满也正常
        // sq.add("aaa");
        // 会一直阻塞，队列是空的，可以理解为队列满了，满的话，放入元素一定会阻塞
        // sq.put("bbb");
        sq.offer("ccc");
        System.out.println(sq);
    }

    public void test2() throws InterruptedException {
        SynchronousQueue sq = new SynchronousQueue();

        // 创建一个线程，取数据
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    try {
                        System.out.println(sq.take());
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        }).start();

        // 创建一个线程，往里面放数据
        new Thread(new Runnable() {

            @Override
            public void run() {
                try {
                    sq.put("aaa");
                    sq.put("bbb");
                    sq.put("ccc");
                    sq.put("ddd");
                    sq.put("eee");
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        }).start();
    }

    public void test3() throws InterruptedException {
        SynchronousQueue sq = new SynchronousQueue();

        // 创建一个线程，取数据
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    try {
                        System.out.println(sq.take());
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        }).start();

        // 创建一个线程，往里面放数据
        new Thread(new Runnable() {

            @Override
            public void run() {
                // 使用add会抛出队列满的异常
                // sq.add("aaa");
                // sq.add("bbb");
                // sq.add("ccc");
                sq.offer("ddd");
            }
        }).start();
    }

    public void test4() throws InterruptedException {
        SynchronousQueue sq = new SynchronousQueue();

        // 创建一个线程，取数据
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    Object poll = null;
                    try {
                        poll = sq.poll(5, TimeUnit.SECONDS);
                        System.out.println(poll);
                        if (poll == null) {
                            break;
                        }
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }

                }
            }
        }).start();

        // 创建一个线程，往里面放数据
        new Thread(new Runnable() {

            @Override
            public void run() {
                try {
                    sq.put("aaa");
                    sq.put("bbb");
                    sq.put("ccc");
                    sq.put("ddd");
                    sq.put("eee");
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        }).start();
    }
}
